package com.itcast.springboot.utils;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.github.kevinsawicki.http.HttpRequest;

import java.util.HashMap;
import java.util.Map;

public class CodeUtils {

    public static Map<String, String> decode(String code) {
        Map<String, String> data = new HashMap<String, String>();
        data.put("appid", "wx89a3dfda138f32b1");//appid
        data.put("secret", "05865b496c95be8bae0f5d93b9204d9e");//安全密钥
        data.put("js_code", code);
        data.put("grant_type", "authorization_code");

        String response = HttpRequest.get("https://api.weixin.qq.com/sns/jscode2session").form(data).body();
        System.out.println("Response was---->: " + response);
        JSONObject obj = JSON.parseObject(response);//将json字符串转换为json对
        System.out.println(obj);
        //过去openId
        String openid = (String) obj.get("openid");
        //获取sessionKey
        String session_key = (String) obj.get("session_key");
        System.out.println("openid--->" + openid);
        System.out.println("session_key--->" + session_key);
        Map<String, String> map = new HashMap<String, String>();
        map.put("openid", openid);
        map.put("session_key", session_key);

        return map;
    }
}
